local wow = wow
local table_concat = table.concat
local find = string.find
local unpack = unpack
local getmetatable = getmetatable
local select = select

module("compat")

local trim, join, split, concat, replace
if wow then
	trim = wow.strtrim
	join = wow.strjoin
	split = wow.strsplit
	concat = wow.strconcat
	replace = wow.strreplace
end
if not join then
	local tbl = {}
	join = function (sep, ...)
		local count = select("#", ...)
		for i = 1, count do
			tbl[i] = select(i, ...)
		end
		for i = #tbl, count + 1, -1 do
			tbl[i] = nil
		end
		return table_concat(tbl, sep)
	end
end
if not concat then
	local tbl = {}
	concat = function (...)
		local count = select("#", ...)
		for i = 1, count do
			tbl[i] = select(i, ...)
		end
		for i = #tbl, count + 1, -1 do
			tbl[i] = nil
		end
		return table_concat(tbl)
	end
end

if not trim then
	trim = function (s)
		return s:gsub("^%s+",""):gsub("%s+$", "")
	end
end
if not split then
	split = function (list, sep)
		local start, pos = 1, find(list, sep)
		if pos then
			local s = {}
			while pos do
				local ss = strip(list:sub(start, pos - 1))
				insert(s, ss)
				start = pos + 1
				pos = find(list, sep, start)
			end
			insert(s, list:sub(start, -1))
			return unpack(s)
		else
			return list
		end
	end
end

if not replace then
	replace = function (s, pattern, subst)
		while true do
			local start, stop = s:find(pattern, nil, true)
			if not start then return s end
			s = s:sub(1, start - 1) .. subst .. s:sub(stop + 1, -1)
		end
	end
end

local sm = getmetatable("").__index
sm.trim = trim
sm.split = split
sm.join = join
sm.replace = replace

if not wow then
	wow = {
		strtrim = trim,
		strsplit = split,
		strjoin = join,
		strreplace = replace,
		strconcat = concat,
	}
end

